//
//  DBChooseIndustryViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDChooseIndustryViewController.h"
#import "BDIndustryModel.h"
#import "UIImage+Tools.h"
#import "BQActivityView.h"
#import "BDGetIndustryListRequest.h"

@interface BDChooseIndustryViewController ()

@property(nonatomic,strong)NSMutableArray *dataList;
@property(nonatomic,copy)void(^industrycode)(NSString *code,NSString *title);

@end

@implementation BDChooseIndustryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitle:@"行业" forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];
    
    self.tableView.backgroundColor = [UIColor hsyDefaultBackgroudColor];
    self.tableView.tableFooterView = [UIView new];
    
    UIView *vi = [[UIView alloc] init];
    vi.backgroundColor = [UIColor clearColor];
    vi.frame = CGRectMake(0, 0, ScreenWidth(), 20.0);
    
    self.tableView.tableHeaderView = vi;
    
    
    [self getIndustryList];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}


#pragma mark - 懒加载datalist -
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}


- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
}


-(void)getIndustrycode:(void(^)(NSString *code,NSString *title))industrycode{
    self.industrycode = industrycode;
}


-(void)getIndustryList{ //获取行业列表
    BDGetIndustryListRequest *task = [[BDGetIndustryListRequest alloc] init];
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];

        if (responseObject) {
            NSMutableArray  *models = [BDIndustryModel mj_objectArrayWithKeyValuesArray:responseObject];
            
            [weakSelf.dataList addObjectsFromArray:models];
            [weakSelf.tableView reloadData];
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
    
}



#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    
    BDIndustryModel *model = self.dataList[indexPath.row];
    cell.textLabel.text = model.title;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.industrycode) {
        BDIndustryModel *model = self.dataList[indexPath.row];
        self.industrycode(model.code,model.title);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
